// (C) Copyright 2012 Kystar. All rights reserved.

`timescale 1ns/100ps
`default_nettype none

module rx_net_check 
(
    input  wire          I_net_in_clk,
    input  wire          I_net_in_en,
    input  wire [  7: 0] I_net_in_data,
    //
    input  wire          I_check_enable,
    output reg           O_net_err_too_short,
    output reg           O_net_err_mac_addr

);

/******************************************************************************
                                <localparams>
******************************************************************************/
localparam
    MAC_ADDR_OFFSET = 'd19,
    MIN_PKG_LENGTH = 64;

/******************************************************************************
                              <internal signals>
******************************************************************************/
reg  [ 11: 0] cnt;
reg  [ 7: 0] mac_addr;
reg  [ 7: 0] last_mac_addr;
wire [ 7: 0] last_mac_addr_plus_one;
reg  net_in_en;

/******************************************************************************
                                <module body>
******************************************************************************/
always @(posedge I_net_in_clk)
    net_in_en <= I_net_in_en;

always @(posedge I_net_in_clk)
    if (!I_net_in_en)
        cnt <= 'd0;
    else if (cnt != 12'hfff)
        cnt <= cnt + 1'b1;

always @(posedge I_net_in_clk)
    if (cnt == MAC_ADDR_OFFSET)
        begin
        mac_addr <= I_net_in_data;
        last_mac_addr <= mac_addr;
        end

assign last_mac_addr_plus_one = last_mac_addr + 1'b1;

always @(posedge I_net_in_clk)
    if (I_net_in_en)
        begin
        O_net_err_too_short <= 1'b0;
        O_net_err_mac_addr <= 1'b0;
        end
    else if (!I_net_in_en && net_in_en && I_check_enable)
        begin
        O_net_err_too_short <= cnt < MIN_PKG_LENGTH;
        O_net_err_mac_addr <= mac_addr != last_mac_addr_plus_one;
        end

endmodule
`default_nettype wire

